home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / cjdates.exe / DTC.C < prev    next >
C/C++ Source or Header  |  1991-07-22  |  3KB  |  93 lines

  1. /*
  2.  *                    dtc.c
  3.  *
  4.  * This program illustrates ways in which "dates.obj" and "datenams.c" can be
  5.  * used.  In response to its question "Gimme a date: " you can supply a Day
  6.  * Number, a Gregorian date (as mm/dd/yyyy) or a Julian date (as ddd/yyyy).
  7.  * Note that the entire year must be given, not just the last two digits.
  8.  * The prgram will convert Day Numbers and Julian dates to Gregorian, and will
  9.  * validate Gregorian dates, showing you what the back conversion gives.
  10.  * To exit the program, just hit ENTER without entering anything.
  11.  *
  12.  *                           {c}Copyright 1991 by Crazy Jack
  13.  *                               All Rights Reserved
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include "dates.h"
  18. #include "datenams.c"
  19.  
  20. static    unsigned int Di, Mi, Yi, Do, Mo, Yo;
  21. static    unsigned long int N;
  22.  
  23. main()
  24. {
  25.  
  26.     int Ct;
  27.     static char InputArea[32];
  28.  
  29.     goto AskEm;
  30.  
  31.     do
  32.     {
  33.         if ( Ct == 1 )        /* ----means we got a Day Number.   */
  34.         {
  35.             if ( ZDate( N, &Yo, &Mo, &Do ) )
  36.                 printf( "Day Number %lu is %s, %s %u, %u,\n"
  37.                        "     and the Julian date is %u/%u.\n",
  38.                           N, DOW( (unsigned int)(N % 7) ),
  39.                             MonthName(Mo), Do, Yo,
  40.                               YDay( Yo, Mo, Do), Yo );
  41.             else
  42.                 printf("Can't convert Day Number %lu.\n", N );
  43.         }
  44.         else if ( Ct == 2 )    /* ----means we got a Julian date.  */
  45.         {
  46.             if ( ZDate( N = ZDay( Di, 1, Mi = (unsigned int)(N) ),
  47.                                  &Yo, &Mo, &Do ) )
  48.                 printf("A%svalid Julian date giving Day "
  49.                     "Number %lu, which is %.3s, %d %.3s, "
  50.                     "%d,\n     and the Julian date is %u/"
  51.                     "%u.\n",    (Mi == Yi) ? " " : "n in",
  52.                           N, DOW( (unsigned int)(N % 7) ),
  53.                             Do, MonthName(Mo), Yo,
  54.                         Yi = YDay( Yo, Mo, Do ), Yo );
  55.                 /* That's a pretty messy "printf" statement,*/
  56.                 /* and not too portable, The setting of "Yi"*/
  57.                 /* by the "YDay" in the last line is to get */
  58.                 /* it ready for the (Mi == Yi) ? test in the*/
  59.                 /* fourth line, and assumes this order for  */
  60.                 /* the evaluation, which the Borland com-   */
  61.                 /* piler uses.  This is what C encourages   */
  62.                 /* in persuit of efficiency.  Almost as bad */
  63.                 /* as APL, eh?  Just be thankful I didn't   */
  64.                 /* have an excuse go pointer-happy on you!  */
  65.             else
  66.                 printf("Can't convert resulting Day Number "
  67.                                 "%lu.\n", N );
  68.         }
  69.         else            /*----means we got a Gregorian date.*/
  70.         {
  71.             Mi = (unsigned int)N;
  72.             if ( ZDate( N = ZDay( Yi, Mi, Di ), &Yo, &Mo, &Do ) )
  73.                 printf("A%svalid Gregorian date giving Day "
  74.                     "Number %lu, which is %s, %u/%u/%u,\n"
  75.                     "     and the Julian date is %u/%u.\n",
  76.                       ( (Mi==Mo) && (Di==Do) ) ? " " : "n in",
  77.                 N, DOW( (unsigned int)(N % 7) ), Mo, Do, Yo,
  78.                               YDay( Yo, Mo, Do ), Yo);
  79.             else
  80.                 printf("Can't convert that date.\n");
  81.         }
  82. AskEm:
  83.         printf("Gimme a date: ");       /* Pop the question.         */
  84.         fflush(stdin);            /* Read the reply.        */
  85.         gets( InputArea );
  86.                     /* Convert numbers in reply,        */
  87.     } while                      /* No conversions means we're done. */
  88.     ( (Ct = sscanf( InputArea, "%U%*[^0-9]%u%*[^0-9]%u", &N, &Di, &Yi ))
  89.                                  && (Ct != EOF) );
  90.  
  91.     return(0);
  92. }
  93.